//
//  UIView+RLLayer.m
//  RealmeLink
//
//  Created by lkx on 2020/8/16.
//  Copyright © 2020 chewyong. All rights reserved.
//

#import "UIView+RLLayer.h"
#import "UIView+Frame.h"

@implementation UIView (RLLayer)

/// 设置圆角
- (void)rlSetCorner:(CGFloat)radius {
    self.layer.cornerRadius = radius;
    self.clipsToBounds = YES;
}

/// 边框的圆角的shapeLayer
/// @param width 边框大小
/// @param backgroundColor 背景颜色
/// @param borderColor 边框颜色
/// @param radius 边框圆角大小
- (CAShapeLayer *)rlsb_setBorderWidth:(CGFloat)width
                      backgroundColor:(UIColor *)backgroundColor
                          borderColor:(UIColor * __nullable)borderColor
                               corner:(CGFloat)radius {
    CAShapeLayer *shapeLayer = [self rlsb_setBorderWidth:width rect:self.bounds backgroundColor:backgroundColor borderColor:borderColor corner:radius];
    
    return shapeLayer;
}

/// 边框的圆角的shapeLayer
/// @param width 边框大小
/// @param backgroundColor 背景颜色
/// @param rect 边框位置
/// @param borderColor 边框颜色
/// @param radius 边框圆角大小
- (CAShapeLayer *)rlsb_setBorderWidth:(CGFloat)width
                                 rect:(CGRect)rect
                      backgroundColor:(UIColor *)backgroundColor
                          borderColor:(UIColor * __nullable)borderColor
                               corner:(CGFloat)radius {
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(radius, radius)];
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.fillColor = backgroundColor.CGColor;
    shapeLayer.strokeColor = borderColor.CGColor;
//    shapeLayer.borderWidth = width;
    shapeLayer.borderColor = borderColor.CGColor;
    shapeLayer.lineWidth = width;
    shapeLayer.path = bezierPath.CGPath;
    
    return shapeLayer;
}

/// 设置带边框的圆角
/// @param width 边框大小
/// @param backgroundColor 背景颜色
/// @param borerColor 边框颜色
/// @param radius 边框圆角大小
- (void)setBorderWidth:(CGFloat)width
       backgroundColor:(UIColor *)backgroundColor
           borderColor:(UIColor * __nullable)borerColor
                corner:(CGFloat)radius {
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(radius, radius)];
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.fillColor = backgroundColor.CGColor;
    shapeLayer.strokeColor = borerColor.CGColor;
    shapeLayer.borderWidth = width;
    shapeLayer.borderColor = borerColor.CGColor;
    shapeLayer.path = bezierPath.CGPath;
    
    [self.layer insertSublayer:shapeLayer atIndex:0];
}

/// 设置带边框的圆角
/// @param backgroundColor 背景颜色
/// @param radius 边框圆角大小
- (CAShapeLayer *)setBackgroundColor:(UIColor *)backgroundColor
                              corner:(CGFloat)radius {
    CAShapeLayer *backgroundLayer = [self setBackgroundColor:backgroundColor rect:self.bounds corner:radius];
    return backgroundLayer;
}

/// 设置圆角
/// @param backgroundColor 背景颜色
/// @param rect 圆角位置
/// @param radius 边框圆角大小
- (CAShapeLayer *)setBackgroundColor:(UIColor *)backgroundColor
                                rect:(CGRect)rect
                              corner:(CGFloat)radius {
    UIBezierPath *backgroundBezierPath =
        [UIBezierPath bezierPathWithRoundedRect:rect
                              byRoundingCorners:UIRectCornerAllCorners
                                    cornerRadii:CGSizeMake(radius, radius)];
    
    CAShapeLayer *backgroundLayer = [CAShapeLayer layer];
    backgroundLayer.fillColor = backgroundColor.CGColor;
    backgroundLayer.path = backgroundBezierPath.CGPath;
//    backgroundLayer.frame = rect;
    return backgroundLayer;
}

/// 设置圆角
/// @param backgroundColor 背景颜色
/// @param corners 圆角定义
/// @param radius 边框圆角大小
- (CAShapeLayer *)rl_setBackgroundColor:(UIColor *)backgroundColor
                   byRoundingCorners:(UIRectCorner)corners
                              corner:(CGFloat)radius {
    UIBezierPath *backgroundBezierPath =
        [UIBezierPath bezierPathWithRoundedRect:self.bounds
                              byRoundingCorners:corners
                                    cornerRadii:CGSizeMake(radius, radius)];
    
    CAShapeLayer *backgroundLayer = [CAShapeLayer layer];
    backgroundLayer.fillColor = backgroundColor.CGColor;
    backgroundLayer.path = backgroundBezierPath.CGPath;
    backgroundLayer.frame = self.bounds;
    return backgroundLayer;
}

/// 设置圆角阴影
/// @param backgroundColor 背景颜色
/// @param corners 圆角定义
/// @param radius 边框圆角大小
/// @param shadowColor 阴影颜色
/// @param shadowOffset 阴影便宜
/// @param shadowOpacity 阴影透明度
- (CAShapeLayer *)rl_setBackgroundColor:(UIColor *)backgroundColor
                      byRoundingCorners:(UIRectCorner)corners
                                 corner:(CGFloat)radius
                            shadowColor:(UIColor *)shadowColor
                           shadowOffset:(CGSize)shadowOffset
                          shadowOpacity:(CGFloat)shadowOpacity {
    CAShapeLayer *shadowLayer = [self rl_setBackgroundColor:backgroundColor byRoundingCorners:corners corner:radius];
    shadowLayer.shadowColor = shadowColor.CGColor;
    shadowLayer.shadowOffset = shadowOffset;
    shadowLayer.opacity = shadowOpacity;
    shadowLayer.shadowRadius = radius;
    
    return shadowLayer;
}

/// 设置带边框的圆角
/// @param progress 进度
/// @param progressColor 进度颜色
/// @param radius 边框圆角大小
- (CAShapeLayer *)setProgress:(CGFloat)progress
                progressColor:(UIColor *)progressColor
                       corner:(CGFloat)radius {
    CGRect progressRect = CGRectMake(0, 0, self.bounds.size.width * progress, self.height);
    UIBezierPath *progressBezierPath = [UIBezierPath bezierPathWithRect:progressRect];
    
//        [UIBezierPath bezierPathWithRoundedRect:progressRect
//                              byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft
//                                    cornerRadii:CGSizeMake(radius, radius)];
    CAShapeLayer *progressLayer = [CAShapeLayer layer];
    progressLayer.fillColor = progressColor.CGColor;
    progressLayer.path = progressBezierPath.CGPath;
    
    return progressLayer;
}

- (CAShapeLayer *)lkx_borderOfDottedLine:(CGFloat)lineLength
                               lineSpace:(CGFloat)lineSpace
                             borderWidth:(CGFloat)borderWidth
                             borderColor:(UIColor *)borderColor
                         backgroundColor:(UIColor *)backgroundColor
                            cornerRadius:(CGFloat)radius {
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:radius];
    CGFloat pattern[] = {4, 2};
    [bezierPath setLineDash:pattern count:1 phase:1];
    
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.path = bezierPath.CGPath;
    layer.borderColor = borderColor.CGColor;
    layer.borderWidth = borderWidth;
    layer.fillColor = backgroundColor.CGColor;
    layer.strokeColor = borderColor.CGColor;
    [layer setLineDashPattern:@[@(lineLength), @(lineSpace)]];
    
    return layer;
}

/// 设置虚线
/// @param lineLength 虚线长度
/// @param lineSpace 虚线间隔
/// @param borderWidth 虚线宽度
/// @param borderColor 边框颜色
/// @param backgroundColor 背景图标
/// @param radius 圆角
- (void)lkx_setBorderOfDottedLine:(CGFloat)lineLength
                        lineSpace:(CGFloat)lineSpace
                      borderWidth:(CGFloat)borderWidth
                      borderColor:(UIColor *)borderColor
                  backgroundColor:(UIColor *)backgroundColor
                     cornerRadius:(CGFloat)radius {
    CAShapeLayer *layer = [self lkx_borderOfDottedLine:lineLength lineSpace:lineSpace borderWidth:borderWidth borderColor:borderColor backgroundColor:backgroundColor  cornerRadius:radius];
    [self.layer insertSublayer:layer atIndex:0];
}

/// 获取渐变背景色
- (CAGradientLayer *)lkx_gradientBackgroundColor:(NSArray<UIColor *> *)colors {
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    NSMutableArray *colorsM = [NSMutableArray arrayWithCapacity:colors.count];
    for (UIColor *color in colors) {
        [colorsM addObject:(__bridge id)color.CGColor];
    }
    gradientLayer.colors = colorsM;
    // 起点和终点表示坐标系位置, (0, 0)表示左上角, (1, 1)表示右下角
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1, 0);
    gradientLayer.frame = self.bounds;
    
    return gradientLayer;
}

/// 设置渐变背景色
- (CAGradientLayer*)lkx_setGradientBackgroundColor:(NSArray<UIColor *> *)colors {
    CAGradientLayer *gradientLayer = [self lkx_gradientBackgroundColor:colors];
    [self.layer insertSublayer:gradientLayer atIndex:0];
    return  gradientLayer;
}

@end
